package graph.element;
import java.util.ArrayList;
import data.*;
import graph.GraphPanel;
import graph.element.shape.*;
import graph.element.axe.*;
import graph.color.*;
public class Graph extends element {
	public Abscisse X;
	public Ordonnee Y;
	public Data Data;
	public Dimensions DimLs;
	public Dimensions DimCs;
	public ArrayList<Shape> Shapes;
	public TypeForm type;
	public int width;
	public int height;
	public boolean value;
	public Color color;
	
	public int getWidth() {
		return width;
	}
	public int getHeight() {
		return height;
	}
	public Data getData() {
		return Data;
	}
	public Dimensions getDimLs() {
		return DimLs;
	}
	public Dimensions getDimCs() {
		return DimCs;
	}
	public Abscisse getAbscisse() {
		return X;
	}
	public Ordonnee getOrdonnee() {
		return Y;
	}
	public Graph(GraphPanel graphingData, int x, int y, Data data, TypeForm type, boolean value, Color color) throws Exception {
		super(graphingData, x, y);
		this.Data = data;
		this.DimLs = Data.getDimLs();
		this.DimCs = Data.getDimCs();
		this.type = type;
		this.value = value; 
		this.color = color;
		genAxes();
		genShapes();
		
	}
	private void genAxes() {
		int maxDimLWidth = DimLs.getMaxWidth(graphingData.getG());
		int maxDimCWidth = DimCs.getMaxWidth(graphingData.getG());
		int x = getX() + maxDimLWidth + 2*SPACE;
		int y = getY() + (DimLs.size() +1) * Ordonnee.INC;
		int x2 = x + (DimCs.size() + 1) * Abscisse.INC;
		int y2 = PAD;
		boolean decal = false;
		if (type == TypeForm.None) {
			decal = true;
		}
		X = new Abscisse(graphingData, decal, x, y, x2, y, DimCs);
		Y = new Ordonnee(graphingData, decal, x, y, x, y2, DimLs);
		this.width = x2 + PAD;
		this.height = y + 2*SPACE + maxDimCWidth + PAD;
	}
	private void genShapes() {
		int xInc = Abscisse.INC;
		int yInc = Ordonnee.INC;
		this.Shapes = new ArrayList<Shape>();
		for(int i = 1; i <= DimLs.size(); i++) {
			for(int j = 1; j <= DimCs.size(); j++) {
				try {
					int val = Data.getVal(DimLs.get(i-1), DimCs.get(j-1));
					int max = Data.getMax();
					Shape shape = null;
					int x = X.getX() + j * xInc;
					int y = Y.getY() - i * yInc;
					switch(type) {
					case Circle:
						shape = new Circle(graphingData, value, color, x,y, xInc, yInc,val, max);
						break;
					case Square:
						shape = new Square(graphingData, value, color, x,y, xInc, yInc, val, max);
						break;
					case None:
						shape = new None(graphingData, value, color, x-xInc/2,y+yInc/2, xInc, yInc, val, max);
						break;
					case NullForm:
						shape = new NullForm(graphingData, value, color, x,y, xInc, yInc, val, max);
						break;
					}
					this.Shapes.add(shape);
				} catch (NullPointerException e) {
					// val not exist in data
				}
			}
		}

	}

	@Override
	public void draw() {
		for(Shape shape: Shapes) {
			shape.draw();
		}
		X.draw();
		Y.draw();
	}

}
